//
//  HZImgItem.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/22.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SDWebImage

class HZImgItem: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        
        contentView.addSubview(imageView)
        contentView.addSubview(gifView)
        
        //约束
        imageView.snp_makeConstraints { (make) -> Void in
            
            //紧贴边界
            make.edges.equalTo(contentView)
        }
        
        gifView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
        
    }

    
    var imgUrl:HZImageModel? {
        
        didSet{
            
            
            let url = NSURL(string: imgUrl!.thumbnail_pic ?? "")

            imageView.sd_setImageWithURL(url)
            
            
            
            //判断是否是 jpg图片
            gifView.hidden = !((imgUrl?.thumbnail_pic ?? "").hasSuffix("gif"))
            
            
        }
    }
    
    
    // MARK: - 懒加载控件
     lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        // 设置内容模式
        imageView.contentMode = .ScaleAspectFill
        // 把超出部分给干掉
        imageView.clipsToBounds = true
        return imageView
    }()

    
    
    private lazy var gifView:UIImageView = {
        
       let gifView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        gifView.sizeToFit()
        
        return gifView
    }()
    
    
}
